package org.zzz.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.zzz.entity.Attendance;
import org.zzz.entity.AttendanceVo;
import org.zzz.entity.Course;
import org.zzz.entity.vo.AttendanceShowVo;
import org.zzz.mapper.*;
import org.zzz.service.AttendanceService;
import org.zzz.service.CourseService;
import org.zzz.service.StudentCourseService;
import org.zzz.utils.MybatisUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author ZZF
 * @Time 2021/04/20 22:44
 */
public class AttendanceServiceImpl implements AttendanceService {
    private AttendanceMapper attendanceMapper;
    private StudentMapper studentMapper;
    private StudentCourseMapper studentCourseMapper;
    private CourseMapper courseMapper;

    {
//        SqlSession sqlSession = MybatisUtil.getSqlSession();
        attendanceMapper = MybatisUtil.getMapper(AttendanceMapper.class);
        studentMapper = MybatisUtil.getMapper(StudentMapper.class);
        studentCourseMapper = MybatisUtil.getMapper(StudentCourseMapper.class);
        courseMapper = MybatisUtil.getMapper(CourseMapper.class);
    }

    @Override
    public boolean insertBatchAttendanceByClassIdAndCourseId(int flag, String cid, String date, String status, String teacherId, String classId) {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<String> studentIdList;
        if (flag == 0) {
            studentIdList = studentMapper.listStudentIdByClassId(classId);
        } else {
            studentIdList = studentCourseMapper.listStudentIdByCourseId(cid);
        }
        for (String studentId : studentIdList) {
            String uuid = UUID.randomUUID().toString();
            int i = attendanceMapper.insertAttendance(new Attendance(uuid, studentId, cid, date, status, teacherId));
            if (i <= 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean insertAttendanceByClass(String sid, String cid, String date, String status, String teacherId, String classId) {
        if (cid != null) {
            return attendanceMapper.insertAttendanceByClassIdAndCourseId(new Attendance(UUID.randomUUID().toString(), sid, cid, date, status, teacherId)) > 0;
        } else {
            return attendanceMapper.insertAttendanceByClassId(new Attendance(UUID.randomUUID().toString(), sid, null, date, status, teacherId)) > 0;
        }
    }

    @Override
    public boolean updateAttendance(Attendance attendance) {
        return attendanceMapper.updateAttendance(attendance) > 0;
    }

    @Override
    public boolean updateAttendanceStatusById(String id, String status) {
        return attendanceMapper.updateAttendanceStatusById(id,status) > 0;
    }

    @Override
    public List<AttendanceVo> listAttendanceByDate(String date) {
        return attendanceMapper.listAttendanceByDate(date);
    }

    @Override
    public List<AttendanceVo> listAttendanceByClass(String classId) {
        return attendanceMapper.listAttendanceByClass(classId);
    }

    @Override
    public List<AttendanceShowVo> listAttendanceBySid(String sid) {
        List<AttendanceShowVo> attendanceShowVos = attendanceMapper.listAttendanceBySid(sid);
        for (AttendanceShowVo attendanceShowVo : attendanceShowVos) {
            String cid = attendanceShowVo.getCid();
            String courseName = "";
            Course course = courseMapper.getCourseById(cid);
            if (course != null){
                courseName = course.getName();
            }
            attendanceShowVo.setCourseName(courseName);
        }
        return attendanceShowVos;
    }

    @Override
    public List<AttendanceVo> listAttendanceBySName(String name) {
        return attendanceMapper.listAttendanceBySName(name);
    }

    @Override
    public AttendanceVo getAttendanceById(String id) {
        return attendanceMapper.getAttendanceById(id);
    }

    @Override
    public List<AttendanceShowVo> listAttendanceVoByTid(String tid) {
        List<AttendanceShowVo> attendanceShowVos = attendanceMapper.listAttendanceVo(tid);
        for (AttendanceShowVo attendanceShowVo : attendanceShowVos) {
            Course course = courseMapper.getCourseById(attendanceShowVo.getCid());
            if (course!= null){
                attendanceShowVo.setCourseName(course.getName());
            }else{
                attendanceShowVo.setCourseName("");
            }
        }
        return attendanceShowVos;
    }

    private void setCourseNameFromAttendance(List<AttendanceShowVo> attendanceShowVos){
        for (AttendanceShowVo attendanceShowVo : attendanceShowVos) {
            Course course = courseMapper.getCourseById(attendanceShowVo.getCid());
            if (course!= null){
                attendanceShowVo.setCourseName(course.getName());
            }else{
                attendanceShowVo.setCourseName("");
            }
        }
    }

    @Override
    public List<AttendanceShowVo> listAttendanceVoByTidAndClassId(String tid, String classId) {
        List<AttendanceShowVo> attendanceShowVos = attendanceMapper.listAttendanceVoByClassId(tid,classId);
        setCourseNameFromAttendance(attendanceShowVos);
        return attendanceShowVos;
    }

    @Override
    public List<AttendanceShowVo> listAttendanceVoByTidAndSname(String tid, String sname) {
        List<AttendanceShowVo> attendanceShowVos = attendanceMapper.listAttendanceVoBySname(tid,sname);
        setCourseNameFromAttendance(attendanceShowVos);
        return attendanceShowVos;
    }

    @Override
    public List<AttendanceShowVo> listAttendanceVoByTidAndDate(String tid, String date) {
        List<AttendanceShowVo> attendanceShowVos = attendanceMapper.listAttendanceVoByDate(tid,date);
        setCourseNameFromAttendance(attendanceShowVos);
        return attendanceShowVos;
    }
}
